#include <mutex>

#include "state.h"

namespace Elements
{

State::State(const State& other)
    : Element(other)
    , m_caption(other.m_caption)
    , m_description(other.m_description)
    , m_workflowId(other.m_workflowId)
    , m_weight(other.m_weight)
    , m_orderNumber(other.m_orderNumber)
    , m_isArchive(other.m_isArchive)
    , m_isQueue(other.m_isQueue)
{
}

bool State::isSame(Element* const other) const
{
    const auto otherElement = dynamic_cast<State const*>(other);
    if (nullptr == otherElement)
        return false;

    return bool(
        m_id == otherElement->m_id
        && m_caption == otherElement->m_caption
        && m_description == otherElement->m_description
        && m_workflowId == otherElement->m_workflowId
        && m_weight == otherElement->m_weight
        && m_orderNumber == otherElement->m_orderNumber
        && m_isArchive == otherElement->m_isArchive
        && m_isQueue == otherElement->m_isQueue
    );
}

Common::Json State::toJson() const
{
    std::shared_lock locker(m_lock);
    Common::Json results;
    results["id"] = m_id;
    results["elementType"] = unsigned(elementType());
    results["caption"] = m_caption;
    results["description"] = m_description;
    results["workflowId"] = m_workflowId;
    results["weight"] = m_weight;
    results["orderNumber"] = m_orderNumber;
    results["isArchive"] = m_isArchive;
    results["isQueue"] = m_isQueue;
    return results;
}

void State::fromJson(const Common::Json& json)
{
    std::shared_lock locker(m_lock);
    m_id = json["id"].get<unsigned>();
    m_caption = json["caption"].get<std::string>();
    m_description = json["description"].get<std::string>();
    m_workflowId = json["workflowId"].get<unsigned>();
    m_weight = json["weight"].get<unsigned>();
    m_orderNumber = json["orderNumber"].get<unsigned>();
    m_isArchive = json["isArchive"].get<bool>();
    m_isQueue = json["isQueue"].get<bool>();
}

Element* State::clone() const
{
    return new State(*this);
}

ElementType State::elementType() const
{
    return ElementType::State;
}

std::string State::caption() const
{
    std::shared_lock locker(m_lock);
    return m_caption;
}

void State::setCaption(const std::string& value)
{
    std::unique_lock locker(m_lock);
    m_caption = value;
}

std::string State::description() const
{
    std::shared_lock locker(m_lock);
    return m_description;
}

void State::setDescription(const std::string& value)
{
    std::unique_lock locker(m_lock);
    m_description = value;
}

unsigned State::workflowId() const
{
    std::shared_lock locker(m_lock);
    return m_workflowId;
}

void State::setWorkflowId(const unsigned value)
{
    std::unique_lock locker(m_lock);
    m_workflowId = value;
}

unsigned State::weight() const
{
    std::shared_lock locker(m_lock);
    return m_weight;
}

void State::setWeight(const unsigned value)
{
    std::unique_lock locker(m_lock);
    m_weight = value;
}

unsigned State::orderNumber() const
{
    std::shared_lock locker(m_lock);
    return m_orderNumber;
}

void State::setOrderNumber(const unsigned value)
{
    std::unique_lock locker(m_lock);
    m_orderNumber = value;
}

bool State::isArchive() const
{
    std::shared_lock locker(m_lock);
    return m_isArchive;
}

void State::setIsArchive(const bool value)
{
    std::unique_lock locker(m_lock);
    m_isArchive = value;
}

bool State::isQueue() const
{
    std::shared_lock locker(m_lock);
    return m_isQueue;
}

void State::setIsQueue(const bool value)
{
    std::unique_lock locker(m_lock);
    m_isQueue = value;
}

} // namespace Elements
